package com.hb.trans.mvp.ui.adapter

import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hb.trans.R
import com.hb.trans.bean.Period
import com.hb.trans.utils.ConvertUtils
import org.jetbrains.anko.backgroundResource
import javax.inject.Inject

class TransRecordAdapter(layoutId: Int) :
    BaseQuickAdapter<Period, BaseViewHolder>(layoutId) {

    private var listener: OnEvaluateClickListener? = null

    @Inject
    constructor() : this(R.layout.item_trans_record)

    override fun convert(helper: BaseViewHolder?, item: Period) {
        helper?.apply {
            setText(R.id.tvYearDate, item.trainingdate)
            setText(R.id.tvTime, "${item.starttime}~${item.endtime}")
            setText(R.id.tvTotalTime, ConvertUtils.convertToHour(item.duration))
            setText(
                R.id.tvValidTime,
                if (item.reviewstatus == 2) {
                    ConvertUtils.convertToPeriodHour(item.lastinstancetime)
                } else {
                    "-"
                }
            )
            setText(R.id.tvCoachName, item.coachName)
            setText(R.id.tvCarCode, item.carName)
            setText(
                R.id.tvTransType,
                mContext.resources.getStringArray(R.array.trans_type)[item.subjcode.toInt() - 1]
            )
            if (item.recheckreason.isNullOrBlank()) {
                setVisible(R.id.llReason, false)
                getView<LinearLayout>(R.id.llReason).visibility = View.GONE
            } else {
                getView<LinearLayout>(R.id.llReason).visibility = View.VISIBLE
                setText(R.id.tvDisqualifyReason, ConvertUtils.convertDisqualifyReason(item.recheckreason))
            }
            val btn = getView<Button>(R.id.btnComplain)
            if (item.rated == 0) {
                btn.text = "评价教练员"
                btn.backgroundResource = R.drawable.shape_btn_bg
                btn.setOnClickListener {
                    listener?.onClick(item)
                }
            } else {
                btn.text = "已评价"
                btn.backgroundResource = R.drawable.shape_btn_bg_disable
                btn.setOnClickListener(null)
            }
        }
    }

    fun setOnEvaluateClickListener(listener: OnEvaluateClickListener) {
        this.listener = listener
    }

    interface OnEvaluateClickListener {
        fun onClick(item: Period)
    }
}